Added log functionality, code clean up and simplify