a working mess
cleaned up code