Fixed memory allocation bug in Layer and tested Layer