added more comments and structured code