Formatted the code with code::blocks. Fixed some bugs