First pass at algorithm to detect active items + general cleanup.