Added checking of data being saved to db + some refactoring