Clean up and complete code

Clean up and fix some code issues