Refactored code a bit and added save functionality