Major refactor: moved a lot of stuff from app -> common.