Code cleanup, small errorhandling added