Added saving functionality and minor bugfixes