greatly simplified the code!