refactored and optimised persistence code