updated the code to be bug-ish free