Added some comments and cleaned code