Added image filesize validation and some refactoring