obvious bug fixes and migrating to small functions