cleaned and restructured the code A BIT