Code clean up and minor fixes