simplified and optimized alignment code