Refactored the split code into functions

This makes the corresponding rules significantly simpler and far less
prone to inadventent bugs afecting only one rule instead of all of them.