improved code, initial implementation of better endpoint analysis