Reworked majority of code.