Added better repl handling. Messy code.
Code cleanup.