Cleanup

Rewrote code to return correct values and check for nulls.