Restructured a bit to make it better. Finalised some functions