Refactored code according to TA comments.  Some features still do not work correctly.