Fix loading bug and add unit tests