More cleanup and error handling. TODO: read variables from file