code cleanup and input handling improvements