added SaveAs functionality and cleaned up some code