added more comments and cleaned code