refactoring the code...still a bunch of bugs for future correction