Moved some mess into self-contained messy functions