Fixed undefined error

Found fix for undefined error.
Still need to clean up the code though.