Fixed some bugs in .onAttach.