respect texture margin
minor refactoring + add tests
minor fixes