Fixes #17: Moving null checks up and adding more checks