Added many defines and fixed outstanding bugs