fixed spacing/tabbing issues
tweaked algorithm