Cleaned up the code a whole lot
Some bugs