code clean up and error fixes: