Reworked the code a little, still needs work.