Fixed layer is null bug (added unit tests and detected another bug)