Code cleanup and loggging