Improved code to add a sub slightly

Now it checks if a sub exists before adding it. Also added comments next
to the complex looking queries because holy shit it's hard to read.