Fixed some nil/type errors; added tests