Fix nil cases and add functional tests