Darkevilmac master

Added code by darkevilmac that fixes a bug and reorganizes things.