Modularize some codes by using function. Basic input checking implemented.