Rebuilded the code, more comments, better structure and some other improvements