some refactoring and correcting layer building