The point of re-inventing the wheel is that you understand it better and aren't just copying stuff you saw other people do. As it is I can't make heads or tails of your code.