fixed input output sanitation and some refactoring #6