added more comments and separated code a little