Revamped for hibernate
Merged Alex's changes