Consolidated code

Still fixes #414