reorginsised file structure and made code more readable. code still leaks memory