Initial Commit

There is a lot going on here and seem like a good candidate  for refactoring and splitting it up.