Simplified by removing the once-only test. Sorted BASE definition
BASE was not being set properly. Replaced readlink / dirname code with simple variable.