added comments, structured main for ease of testing