Fixed various bugs because of wrong variable names used.