pretty much a complete rewrite

revised everything to be hyper-intelligent (with comments to explain
along the way), so this will work regardless of device and settings,
although it looks a bit messier. confusing logic and such be gone!