clean up code, attempt some bug fixes