Code clean up & minor bug fixes