cleaned up code and fixed a few issues, error handling working