some refactoring + patch layer generation