made some preliminary changes, cleaned up and added comments