Merged Jim's fork back, features code is a bit cleaner but still a work in progress