faster implementation by removing numpy operations