refctored to fix bug.  Moved