improved input handling. now it is immune to invalid input