fix input scanning and refactor