Cleaned up variable names. Corrected an issue where the incorrect stage variable was being grabbed. Added bounds checking.