fixed bug in Layer class, refactored code