moved log, cleaned up code for readability