move everything into functions, add sanity check and parsing functions