MAJOR changes to the code. take a look